package ModelLayer;

import java.util.*;
/**
 * Write a description of class Adressebog here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AddressBook
{
    private ArrayList<Person> addressBook;
    public static AddressBook instance;

    public AddressBook()
    {
        addressBook = new ArrayList<Person>();
    }
    
    public static AddressBook getSingleton()
    {
        if(instance == null)
        {
            instance = new AddressBook();
        }
        return instance;
    }

    public Person getPerson(int id)
    {
        int index = 0;
        boolean found = false;
        while(index < addressBook.size() && !found)
        {
            if(addressBook.get(index).getId() == id)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        return addressBook.get(index);
    }
    
    public void updatePerson(int id, String name,String address, String postalCode, String city, String phoneNr)
    {
        int index = 0;
        boolean found = false;
        while(index < addressBook.size() && !found)
        {
            if(addressBook.get(index).getId() == id)
            {
                if(name.length() > 0)
                {
                    addressBook.get(index).setName(name); 
                }
                if(address.length() > 0)
                {
                    addressBook.get(index).setAddress(address);
                }
                if(postalCode.length() > 0)
                {
                    addressBook.get(index).setZipCode(postalCode); 
                }
                if(city.length() > 0)
                {
                    addressBook.get(index).setCity(city);
                }
                if(phoneNr.length() > 0)
                {
                    addressBook.get(index).setPhoneNr(phoneNr);
                }
                found = true;
            }
            else
            {
                index++;
            }
        }
    }
    
    public void deletePerson(int id)
    {
        int index = 0;
        boolean found = false;
        while(index < addressBook.size() && !found)
        {
            if(addressBook.get(index).getId() == id)
            {
                addressBook.remove(index);
                found = true;
            }
            else
            {
                index++;
            }
        }
    }
    
    public void createPerson(Person person)
    {
        addressBook.add(person);
    }
}
